package com.xudow.app.network;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.xudow.app.XApplication;
import com.xudow.app.newui.zutils.LsUtils;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/5/20.
 */
public class OkHttpClientManager {
	

    OkHttpClient okHttpClient;

    AjaxCallBack acb;
//    AppContext appContext=(AppContext)AppContext.getContext();
Context activity;
    Handler handler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==1){

               String s= (String) msg.obj;
                try {

//                    Log.i("lishuai","s>>>>>>>>>>"+s.toString());
                                acb.onSucces(s);

                }catch (Exception e){
                }

            }
            if(msg.what==2){
                String s= (String) msg.obj;

//                Toast.makeText(activity,s,Toast.LENGTH_SHORT).show();
                LsUtils.pdDismiss();

            }
        }
    };


    public synchronized void getAsyn(final Context activity, String url, final AjaxCallBack acb){
        this.acb=acb;
        this.activity=activity;
    final Request request = new Request.Builder()
                    .url(url)
                    .build();

    okHttpClient=new OkHttpClient();
     Call call = okHttpClient.newCall(request);

        try {


    call.enqueue(new Callback()
            {
                @Override
                public void onFailure(Call call, IOException e) {
//                    String s= request.body().toString();
////                    Util.log("newwork", "request *********" + s);
//
////                    Util.toast(activity, s);
                    Message msg=new Message();
                    msg.obj="网络异常！";
                    msg.what=2;
                    handler.sendMessage(msg);

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String s=response.body().string();

//                    Log.i("ordersss","s"+response.toString());
                    Message msg=new Message();
                    msg.obj=s;
                    msg.what=1;
                    handler.sendMessage(msg);
                }

                });
        }catch (Exception e){
//            Toast.makeText(activity,"网络异常！",Toast.LENGTH_SHORT).show();
        }
    }
    

    public synchronized void getAsyn(final Context activity, String url, Map<String,String> map, final AjaxCallBack acb){

//        Log.i("lishuai", "url>>>>>" + url);
        this.activity=activity;
        this.acb=acb;
        FormBody.Builder builder=new FormBody.Builder();

        for(Map.Entry<String, String> entry : map.entrySet()){


         Log.i("lishuai", "entry.getKey()>>>>>" + entry.getKey() + ">>entry.getValue()>>>>>" + entry.getValue());
      if(entry.getValue()!=null){
         builder.add(entry.getKey(), entry.getValue());
       }

      }

        final Request request= new Request.Builder().url(url).post(builder.build()).build();

           okHttpClient=new OkHttpClient();

           okHttpClient.newCall(request).enqueue(new Callback() {
               @Override
               public void onFailure(Call call, IOException e) {
//                   String s= request.body().toString();
//                   Util.log("newwork", "request *********" + s);

//                   Util.toast(activity, s);
                   Message msg=new Message();
                   msg.obj="网络异常！";
                   msg.what=2;
                   handler.sendMessage(msg);
               }

               @Override
               public void onResponse(Call call, Response response) throws IOException {
                   String s=response.body().string();
//                   Util.log("newwork", "response *********" + s);
//                   Log.i("lishuai","s>>>>>>>>>>"+s.toString());
                   Message msg=new Message();
                   msg.obj=s;
                   msg.what=1;
                   handler.sendMessage(msg);
               }
               
              });
      }


}
